package com.topscomm.qem;

import com.topscomm.cbo.service.ICboItemDetailsService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.qem.service.FilePreviewService;
import com.topscomm.tap.ITapController;
import com.topscomm.tap.TapController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class QemController<T extends QemEntity, TService extends IQemService<T, ?>>
        extends TapController<T, TService> implements ITapController<T, TService> {
    @Autowired
    protected ICboItemDetailsService cboItemDetailsService;
    @Autowired
    protected FilePreviewService filePreviewService;
    /**
     *
     * @description: 查询编码
     * @param dataMap
     * @param paraMap
     * @throws Exception
     * @author: hudeyong
     * @date: 2023-01-12
     * @modify:
     */
    @GetMapping(value = "/queryDataCode")
    public ResponseResult<Object> queryDataCode(@RequestParam(required = false) Map<String, Object> paraMap) {
        try {
            paraMap = BaseUtil.decodeSecureMap(paraMap);
            String datacode = ConvertUtil.convertToString(paraMap.get("datacode"));
            StringBufferProxy sql = new StringBufferProxy();
            List<String> filedList = new ArrayList<>();
            filedList.add("id");
            filedList.add("name");
            filedList.add("code");
            filedList.add("enabled");
            sql.appendSingle("ITEMID = (SELECT id FROM CBOITEMS WHERE code = '{0}') order by code asc",datacode);
            List<Map<String, Object>> resultList = cboItemDetailsService.queryMapFieldsByWhere(sql.toString(), filedList);
            return ResponseResult.ok(resultList);
        } catch (Exception ex) {
            return handleControllerException(ex);
        }
    }
    /**
	 * @Description:    下载合同模板并预览
	 * @Author:         mengmeng
	 * @Date:           2021/9/16 14:42
	 * @Version:        1.0
	 */
	@PostMapping(value = "/previewDownLoad")
	public void download(@RequestBody Map<String, Object> paraMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Map<String, Object> resultMap = filePreviewService.executeDownload(paraMap);
			// 设置文件名字(给要下载文件重命名);
			String fileName = ConvertUtil.convertToString(resultMap.get("attFileName"));
			response.setContentType("application/octet-stream");
//			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			InputStream inStream = BaseUtil.convertByteToStream((byte[]) resultMap.get("attInputStream"));
			OutputStream outStream = response.getOutputStream();
			byte[] bytes = new byte[2048];
			int len;
			while ((len = inStream.read(bytes)) != -1) {
				outStream.write(bytes, 0, len);
			}
			inStream.close();
		} catch (Exception e) {
			response.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
		}
	}
}
